/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.simba.evaluation;

import java.util.HashMap;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 *
 * @author ngonga
 */
public class Eval {

    public static double getPrecision(Set<String> reference, Set<String> predicted) {
        if (predicted.isEmpty()) {
            return 0;
        }
        double count = 0;
        for (String e : predicted) {
            if (reference.contains(e)) {
                count++;
            }
        }
        return count / predicted.size();
    }

    public static double getOverlap(Set<String> reference, Set<String> predicted) {
        if (predicted == null || reference == null) {
            return 0;
        }
        if (predicted.isEmpty()) {
            return 0;
        }
        double count = 0;

        for (String e : predicted) {
            if (reference.contains(e)) {
                count++;
            }
        }
        return count;
    }

    public static double getRecall(Set<String> reference, Set<String> predicted) {
        if (reference.isEmpty()) {
            return 1;
        }
        double count = 0;
        for (String e : predicted) {
            if (reference.contains(e)) {
                count++;
            }
        }
        return count / reference.size();
    }

    public static double getFScore(Set<String> reference, Set<String> predicted) {
        return 2.0 / ((1 / getPrecision(reference, predicted)) + 1 / getRecall(reference, predicted));
    }

    public static void getDiff(HashMap<String, TreeSet<String>> reference,
            HashMap<String, TreeSet<String>> predicted) {
        String s;
        for (String key : predicted.keySet()) {
            s = key + "\t<PRE>\t";
            TreeSet<String> p = predicted.get(key);
            TreeSet<String> r = reference.get(key);
            for(String pr: p)
            {
                if(!r.contains(pr)) s = s+pr+";";
            }
            s = s+"\t<REF>\t";
            for(String ref: r)
            {
                if(!p.contains(ref)) s = s+ref+";";
            }
            
            System.out.println(s);
        }
    }
    public static double getMicroAveragePrecision(HashMap<String, TreeSet<String>> reference,
            HashMap<String, TreeSet<String>> predicted) {
        double count = 0;
        double totalPredicted = 0;
        for (String key : predicted.keySet()) {
           // System.out.println(key + "\t<REF>\t"+reference.get(key)+"\t<PRE>\t"+predicted.get(key));
            count = count + getOverlap(reference.get(key), predicted.get(key));
            totalPredicted = totalPredicted + predicted.get(key).size();
        }
        return count / totalPredicted;
    }

    public static double getMicroAverageRecall(HashMap<String, TreeSet<String>> reference,
            HashMap<String, TreeSet<String>> predicted) {
        double count = 0;
        double totalReference = 0;

        for (String key : reference.keySet()) {
            count = count + getOverlap(reference.get(key), predicted.get(key));
            totalReference = totalReference + reference.get(key).size();
        }
        return count / totalReference;
    }

    public static double getMacroAveragePrecision(List<Set<String>> reference,
            List<Set<String>> predicted) {
        double count = 0;
        for (int i = 0; i < reference.size(); i++) {
            count = count + getPrecision(reference.get(i), predicted.get(i));
        }
        return count / reference.size();
    }

    public static double getMacroAverageRecall(List<Set<String>> reference,
            List<Set<String>> predicted) {
        double count = 0;
        for (int i = 0; i < reference.size(); i++) {
            count = count + getRecall(reference.get(i), predicted.get(i));
        }
        return count / reference.size();
    }
}
